package com.pshcp.patient.api;

import com.framework.base.PageList;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.common.config.CustomConf;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.tool.CommonTool;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.PatientFavor;
import com.pshcp.patient.domain.patientfavor.ao.PatientFavorAO;
import com.pshcp.patient.domain.patientfavor.ao.PatientFavorListAO;
import com.pshcp.patient.domain.patientfavor.vo.PatientFavorVO;
import com.pshcp.patient.tool.PatientFavorTools;
import com.pshcp.service.DoctorService;
import com.pshcp.service.PatientFavorService;
import com.pshcp.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = {"患者端-关注医生"})
@Slf4j
@RestController
@RequestMapping("/api/patient/favor")
public class PatientFavorAPI {

    @Autowired
    private PatientService patientService;

    @Autowired
    private PatientFavorService favorService;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private CustomConf customConf;

    @ApiOperation("关注/取消关注医生")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.ADD, desc = "关注/取消关注医生")
    @PostMapping("/favorDoctor")
    public ResultDTO favorDoctor(@RequestBody PatientFavorAO favorAO) throws BizException {
        PatientFavor favor = PatientFavorTools.buildModel(favorAO.getDoctorId());
        if(favorAO.getActionType() == 0){
            favorService.insert(favor);
        }else{
            favorService.delByDoctorId(favorAO.getDoctorId());
        }

        return ResultBuilder.success();
    }

    @ApiOperation("关注列表")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "关注列表")
    @PostMapping("/favorList")
    public ResultDTO<PageList<PatientFavorVO>> favorList(PatientFavorListAO listAO) {
        PageList<PatientFavor> pageList = favorService.list(PatientFavorTools.buildFavorListAO2DTO(listAO));
        PageList<PatientFavorVO> favorVOPageList = CommonTool.buildPageList(pageList, PatientFavorVO.class);
        if(CollectionUtils.isEmpty(favorVOPageList.getItems())){
            return ResultBuilder.success(favorVOPageList);
        }
        // 补充医生详细信息
        List<Long> doctorIds = favorVOPageList.getItems().stream().map(f->f.getDoctorId()).collect(Collectors.toList());
        List<Doctor> doctors = doctorService.findByIds(doctorIds);
        if(CollectionUtils.isEmpty(doctors)){
            return ResultBuilder.success(favorVOPageList);
        }
        Map<Long, Doctor> idAndDoctorMap = doctors.stream().collect(Collectors.toMap(d->d.getId(), d->d));
        favorVOPageList.getItems().forEach(f->{
            PatientFavorTools.buildDoctor2FavorVO(f, idAndDoctorMap.get(f.getDoctorId()), customConf.getFileUrl());
        });
        return ResultBuilder.success(favorVOPageList);
    }

}
